package com.tangr.normaladapter.recyclerview;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.tangr.normaladapter.R;
import com.tangr.normaladapter.ViewHolder;

import java.util.List;

/**
 * Created by tangr on 2016/6/7.
 */
public abstract class RecyclerAdapter<T> extends RecyclerView.Adapter<ViewHolder>{
    protected List<T> mList;
    protected Context mContext;
    protected int mLayoutId;
    protected OnItemClickListener mListener;
    protected final static int HEADER_TYPE = 100;
    protected final static int FOOTER_TYPE = 200;
    protected final static int LOADMORE_TYPE = 300;
    protected int header_layout = -1;
    protected int footer_layout = -1;
    private boolean footShowed = false;
    private boolean enableLoadmore = false;

    public RecyclerAdapter(Context mContext, List<T> mList, int mLayoutId) {
        this.mList = mList;
        this.mContext = mContext;
        this.mLayoutId = mLayoutId;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder viewHolder = ViewHolder.get(mContext, null, parent, getLayout(viewType));
        if(viewType!=HEADER_TYPE&&viewType!=FOOTER_TYPE&&viewType!=LOADMORE_TYPE&&isEnabled(viewType))
            setListener(parent, viewHolder);
        return viewHolder;
    }

    protected int getLayout(int viewType) {
        if(viewType==HEADER_TYPE)
            return header_layout;
        if(viewType==FOOTER_TYPE)
            return footer_layout;
        if(viewType==LOADMORE_TYPE)
            return R.layout.pull_to_refresh_footer;
        return mLayoutId;
    }

    public void setHeader(int headerLayout){
        this.header_layout = headerLayout;
//        notifyItemInserted(0);
    }

    public void setFooter(int footerLayout){
        this.footer_layout = footerLayout;
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getAdapterPosition();
        return header_layout == -1 ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        int count = mList.size();
        if (header_layout != -1)
            count++;
        if (footer_layout != -1 && !enableLoadmore)
            count++;
        if (footShowed)
            count++;
//        return (header_layout == -1 ? mList.size() : mList.size() + 1) + (footShowed ? 1 : 0);
        return count;
    }

    public boolean isEnabled(int viewType)
    {
        return true;
    }

    protected void setListener(final ViewGroup parent, final ViewHolder viewHolder) {
        View view = viewHolder.getConvertView();
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null)
                {
                    int position = getRealPosition(viewHolder);
                    mListener.onItemClick(parent,v,mList.get(position),position);
                }
            }
        });
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mListener != null) {
                    int position = getRealPosition(viewHolder);
                    return mListener.onItemLongClick(parent, v, mList.get(position), position);
                }
                return false;
            }
        });
    }

    @Override
    public void onBindViewHolder (ViewHolder holder,int position) {
        if(holder.getItemViewType()==HEADER_TYPE){
            return;
        }else if(holder.getItemViewType()==FOOTER_TYPE){
            return;
        }else if(holder.getItemViewType()==LOADMORE_TYPE){
//            if (holder.itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) {
//                StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
//                params.setFullSpan(true);
//            }
            return;
        }else {
            setView(holder, mList.get(getRealPosition(position)));
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(header_layout!=-1 && position==0)
            return HEADER_TYPE;
        if (footer_layout!=-1 && !enableLoadmore && position==getItemCount()-1){
            return FOOTER_TYPE;
        }
        if(footShowed && position==getItemCount()-1){
//            onLoadMoreStateChanged(true);
            return LOADMORE_TYPE;
        }
        return setItemViewType(getRealPosition(position));
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return (getItemViewType(position) == HEADER_TYPE || getItemViewType(position) == FOOTER_TYPE || getItemViewType(position) == LOADMORE_TYPE)
                            ? gridManager.getSpanCount() : 1;
                }
            });
        }
    }

    @Override
    public void onViewAttachedToWindow(ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        if(lp != null
                && lp instanceof StaggeredGridLayoutManager.LayoutParams) {
            StaggeredGridLayoutManager.LayoutParams p = (StaggeredGridLayoutManager.LayoutParams) lp;
            p.setFullSpan((getItemViewType(holder.getAdapterPosition()) == HEADER_TYPE || getItemViewType(holder.getAdapterPosition()) == LOADMORE_TYPE) ? true : false);
        }
    }

    private int getRealPosition(int position){
        return position - (header_layout==-1 ? 0 : 1);
    }

    public void onLoadMoreStateChanged(boolean isShown) {
        this.footShowed = isShown;
        if (isShown) {
            notifyItemInserted(getItemCount());
        } else {
            notifyItemRemoved(getItemCount());
        }
    }

    public void setEnableLoadmore(boolean enableLoadmore){
        this.enableLoadmore = enableLoadmore;
    }

    protected int setItemViewType(int position) {
        return 0;
    }

//    @Override
//    public int getItemCount() {
//        return mList.size();
//    }

    public abstract void setView(ViewHolder holder, T item);

    public void setOnItemClickListener(OnItemClickListener listener){
        this.mListener = listener;
    }

    protected int getPosition(RecyclerView.ViewHolder viewHolder)
    {
        return viewHolder.getAdapterPosition();
    }
}
